const app = getApp()
Component({
  data: {
    userInfo: '',
    formData: {
    },
    error: '',
    isResidentItems: [{
        name: '是',
        value: '1'
      },
      {
        name: '否',
        value: '0'
      }
    ],
    sexItems: [{
      name: '男',
      value: '1'
    },
    {
      name: '女',
      value: '2'
    }],
    rules: [{
      name: 'name',
      rules: {
          required: true,
          message: '姓名必填'
      },
    }, {
        name: 'userId',
        rules: [{
            required: true,
            message: '身份证必填'
        },{
            minlength: 18,
            message: '身份证为18位'
        },{
            maxlength: 18,
            message: '身份证应为18位'
        }],
    }, {
        name: 'phone',
        rules: [{
            required: true,
            message: '电话必填'
        },{
            mobile: true,
            message: '请仔细检查手机号'
        }],
    }, {
        name: 'address',
        rules: {
            required: true,
            message: '地址必填'
        },
    }, {
        name: 'isResident',
        rules: {
            required: true,
            message: '是否住户必选'
        },
    }, {
        name: 'sex',
        rules: {
            required: true,
            message: '性别必选'
        },
    }]
  },

  methods: {
    onLoad: function () {
      this.setData({
        // 这里是模拟数据
        userInfo: wx.getStorageSync('userInfo'),
        [`formData.openid`]: wx.getStorageSync('userInfo').openid,
        [`formData.skey`]:wx.getStorageSync('userInfo').skey,
        [`formData.name`]: wx.getStorageSync('userInfo').name,
        [`formData.userId`]: wx.getStorageSync('userInfo').userId,
        [`formData.phone`]: wx.getStorageSync('userInfo').phone,
        [`formData.address`]: wx.getStorageSync('userInfo').address
      })

    },
 
    isResidentChange: function (e) {
      var isResidentItems = this.data.isResidentItems;
      for (var i = 0, len = isResidentItems.length; i < len; ++i) {
          isResidentItems[i].checked = isResidentItems[i].value == e.detail.value;
      }
      this.setData({
          isResidentItems: isResidentItems,
          [`formData.isResident`]: e.detail.value
      });
    },
    sexChange: function (e) {
      var sexItems = this.data.sexItems;
      for (var i = 0, len = sexItems.length; i < len; ++i) {
          sexItems[i].checked = sexItems[i].value == e.detail.value;
      }
      this.setData({
          sexItems: sexItems,
          [`formData.sex`]: e.detail.value
      });
    },
    formInputChange(e) {
        const { field } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },
    submitForm() {
        this.selectComponent('#form').validate((valid, errors) => {
            //是否有错误
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    this.setData({
                        error: errors[firstError[0]].message,
                    })
                }else{
                }
            } else {
                const that = this 
                wx.request({
                    url: app.globalData.serverUrl + '/user',
                    method: 'put',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                        openid: that.data.formData.openid,
                        skey: that.data.formData.skey,

                        name: that.data.formData.name,
                        phone: that.data.formData.phone,
                        address: that.data.formData.address,
                        userId: that.data.formData.userId,
                        isResident: parseInt(that.data.formData.isResident),
                        sex: parseInt(that.data.formData.sex)
                    },
                    success: function (res) {
                        if (res.data.status == 200) {
                            wx.showToast({
                                title: '修改成功',
                                duration: 1500
                            })
                            that.data.userInfo.name = that.data.formData.name
                            that.data.userInfo.phone = that.data.formData.phone
                            that.data.userInfo.address = that.data.formData.address
                            that.data.userInfo.userId = that.data.formData.userId
                            that.data.userInfo.isResident = parseInt(that.data.formData.isResident)
                            that.data.userInfo.sex = parseInt(that.data.formData.sex)
                           
                            wx.setStorageSync('userInfo', that.data.userInfo)
                            if(!that.data.userInfo.role){
                                wx.redirectTo({
                                    url: '/pages/home-client/home-client',
                                })
                            }else{
                                wx.redirectTo({
                                    url: '/pages/home-checker/home-checker',
                                })
                            }
                        } else {
                            wx.showToast({
                                title: '更改失败,请确保填写数据正确',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    },
                    fail: function (error) {
                        //调用服务端登录接口失败
                        wx.showToast({
                            title: '请确保网络通畅',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                })

            }
        })
        
    }

  }
});